package com.rgsx.controller;

import com.rgsx.entiy.Position;
import com.rgsx.service.OrderService;
import com.rgsx.utils.JWTUtil;
import com.rgsx.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@ResponseBody
@RequestMapping("/order")
@CrossOrigin
@Api(tags = "订单相关的API")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/origination")
    @ApiOperation(value = "乘客发起打车请求(乘客)",notes = "乘客发起打车请求")
    public ResultVo OrderOrigination(String positionStartLatitude,
                                     String positionStartLongitude,
                                     String positionEndLatitude,
                                     String positionEndLongitude,
                                     @RequestHeader("token") String token){
        return orderService.OrderOrigination(
                new Position(positionStartLatitude,positionStartLongitude),
                new Position(positionEndLatitude,positionEndLongitude),
                token);
    }

    @GetMapping("/getNearbyOrder")
    @ApiOperation(value = "获取附近打车订单(司机)",notes = "获取附近打车订单")
    public ResultVo getNearbyOrder(String positionLatitude,
                                   String positionLongitude,
                                   @RequestHeader("token") String token){
        long driverId = JWTUtil.getUserId(token);
        return orderService.getNearbyOrder(driverId,
                new Position(positionLatitude,positionLongitude));
    }

    @GetMapping("/getAllOrder")
    @ApiOperation(value = "拿到进行中订单(用户)",notes = "拿到进行中订单")
    public ResultVo getAllOrder(@RequestHeader("token") String token){
        long userId = JWTUtil.getUserId(token);
        return orderService.getAllOrder(userId);
    }

    @GetMapping("/ReceivingOrders")
    @ApiOperation(value = "接单(司机)",notes = "接单")
    public ResultVo ReceivingOrders(@RequestHeader("token") String token,long orderId){
        long driverId = JWTUtil.getUserId(token);
        return orderService.ReceivingOrders(orderId,driverId);
    }

    @GetMapping("/GetOnTheCar")
    @ApiOperation(value = "乘客上车(司机)",notes = "乘客上车(司机)")
    public ResultVo GetOnTheCar(@RequestHeader("token") String token,
                                String userName,long orderId){
        long driverId = JWTUtil.getUserId(token);

        return orderService.GetOnTheCar(orderId,userName);
    }

    @GetMapping("/getDriverOrder")
    @ApiOperation(value = "获取司机的进行中订单(司机)",notes = "获取司机的进行中订单(司机)")
    public ResultVo getDriverOrder(@RequestHeader("token") String token){
        long driverId = JWTUtil.getUserId(token);

        return orderService.getDriverOrder(driverId);
    }

    @GetMapping("/orderOver")
    @ApiOperation(value = "订单结束(司机)",notes = "订单结束(司机)")
    public ResultVo orderOver(long orderId,
                              String positionLatitude,
                              String positionLongitude){
        Position position = new Position(positionLatitude,positionLongitude);

        return orderService.orderOver(orderId,position);
    }


}
